
//工具集合Tools
window.T = {};

// 获取请求参数
// 使用示例
// location.href = http://localhost/index.html?id=123
// T.p('id') --> 123;
var url = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)return unescape(r[2]);
    return null;
};
T.p = url;

//请求前缀
var baseURL = "/";

//登录token
var token = localStorage.getItem("token");
if (!token) {
    location.href = baseURL + 'login.html';
}

//jquery全局配置
$.ajaxSetup({
    dataType: "json",
    cache: false,
    headers: {
        "token": token
    },
    xhrFields: {
        withCredentials: true
    },
    complete: function (xhr) {
        //token过期，则跳转到登录页面
        if (xhr.responseJSON.code == 401) {
            location.href = baseURL + 'login.html';
        }
    }
});


//权限判断
function hasPermission(permission) {
    if (window.parent.permissions.indexOf(permission) > -1) {
        return true;
    } else {
        return false;
    }
}

function toPercent1(point) {
    var str = Number(point * 100).toFixed(1);
    str += "%";
    return str;
}

function toPercent(data) {
    var strData = parseFloat(data) * 10000;
    strData = Math.round(strData);
    strData /= 100.00;
    var ret = strData.toString() + "%";
    return ret;
}

//判断是否为空
function isBlank(value) {
    return !value || !/\S/.test(value)
}

function isEmpty(value){
    return Object.keys(value).length === 0;
}

/*
 * 获取URL参数
 **/
var GetQueryString = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return (r[2]);
    return null;
};

/*
* 时间格式化
* */

function formatTime(time){
    if(!time){
        return '';
    }
//    时间转时间戳
    var timeStamp = '';
    if(typeof time == 'number'){
       timeStamp = new Date(time);
    }
    var year = timeStamp.getFullYear();
    var month = timeStamp.getMonth() + 1;
    var day = timeStamp.getDate();

    return year + " 年 " + month + " 月 " + day + " 日";
}